package cn.maszy.service.impl.projectmanagement;

import java.util.Date;
import java.util.List;
import java.util.UUID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.maszy.common.Pager;
import cn.maszy.common.SessionFactory;
import cn.maszy.dao.projectmanagement.B_T_Ufp_EngineerMaterialMapper;
import cn.maszy.dao.projectmanagement.B_T_Ufp_EngineeringMapper;
import cn.maszy.model.projectmanagement.B_T_Ufp_EngineerCategory;
import cn.maszy.model.projectmanagement.B_T_Ufp_EngineerCategoryExample;
import cn.maszy.model.projectmanagement.B_T_Ufp_EngineerMaterial;
import cn.maszy.model.projectmanagement.B_T_Ufp_EngineerMaterialExample;
import cn.maszy.model.projectmanagement.B_T_Ufp_Engineering;
import cn.maszy.model.projectmanagement.B_T_Ufp_EngineerMaterialExample.Criteria;
import cn.maszy.service.projectmanagement.B_T_Ufp_EngineerMaterialService;
@Service
public class B_T_Ufp_EngineerMaterialServiceImpl implements B_T_Ufp_EngineerMaterialService {
	@Autowired
	private B_T_Ufp_EngineerMaterialMapper b_T_Ufp_EngineerMaterialMapper;
	@Autowired
	private B_T_Ufp_EngineeringMapper b_T_Ufp_EngineeringMapper;
	
	/* (non-Javadoc)
	 * @see cn.maszy.service.investment.B_T_Ufp_AdminService#update(cn.maszy.model.investment.B_T_Ufp_EngineerMaterial, cn.maszy.model.investment.B_T_Ufp_AdminExample)
	 */
	public int update(B_T_Ufp_EngineerMaterial b_T_Ufp_EngineerMaterial) {
		// TODO Auto-generated method stub
		 if(b_T_Ufp_EngineerMaterial.getUuid()==null||"".equals(b_T_Ufp_EngineerMaterial.getUuid())){
			//查询是否有相同材料名
			 B_T_Ufp_EngineerMaterialExample example=new B_T_Ufp_EngineerMaterialExample();
			 Criteria criteria = example.createCriteria();
				if(b_T_Ufp_EngineerMaterial!=null)
				{
					if(b_T_Ufp_EngineerMaterial.getEngineeringUuid()!=null&&!"".equals(b_T_Ufp_EngineerMaterial.getEngineeringUuid())){
						criteria.andEngineeringUuidEqualTo(b_T_Ufp_EngineerMaterial.getEngineeringUuid());
					}
					if(b_T_Ufp_EngineerMaterial.getMaterialName()!=null&&!"".equals(b_T_Ufp_EngineerMaterial.getMaterialName())){
						criteria.andMaterialNameEqualTo(b_T_Ufp_EngineerMaterial.getMaterialName());
					}
				}
				else
				{
					criteria.andUuidIsNotNull();
				}
				example.or(criteria);
				// 取得总的记录条数
			List<B_T_Ufp_EngineerMaterial>  list=b_T_Ufp_EngineerMaterialMapper.selectByExample(example);
			 if(list.size()>=1)
			 {
				 //发现有相同的话，就把查出的uuid设置给新来的数据。并执行修改
				 b_T_Ufp_EngineerMaterial.setUuid(list.get(0).getUuid());
				 //将2条重复数据的数量和总金额相加
				 int j =b_T_Ufp_EngineerMaterial.getQuantity();
				 int k =list.get(0).getQuantity();
				 b_T_Ufp_EngineerMaterial.setQuantity(j+k);
				 float totalAmount = list.get(0).getTotalAmount();
				 float totalAmount1= b_T_Ufp_EngineerMaterial.getTotalAmount();
				 b_T_Ufp_EngineerMaterial.setTotalAmount(totalAmount+totalAmount1);
				 //执行修改操作
				 b_T_Ufp_EngineerMaterial.setUpdateTime(new Date());
				 b_T_Ufp_EngineerMaterial.setUpdateUser(SessionFactory.getSessionUserRealNam());
				 return b_T_Ufp_EngineerMaterialMapper.updateByPrimaryKeySelective(b_T_Ufp_EngineerMaterial);
			 }
			 b_T_Ufp_EngineerMaterial.setUuid(UUID.randomUUID().toString());
			 b_T_Ufp_EngineerMaterial.setIsDelete(0);
			 b_T_Ufp_EngineerMaterial.setCreateTime(new Date());
			 b_T_Ufp_EngineerMaterial.setCreateUser(SessionFactory.getSessionUserRealNam());
			 return b_T_Ufp_EngineerMaterialMapper.insert(b_T_Ufp_EngineerMaterial);
		 }else{
			 b_T_Ufp_EngineerMaterial.setUpdateTime(new Date());
			 b_T_Ufp_EngineerMaterial.setUpdateUser(SessionFactory.getSessionUserRealNam());
			 return b_T_Ufp_EngineerMaterialMapper.updateByPrimaryKeySelective(b_T_Ufp_EngineerMaterial);
		 }
	}

	/* (non-Javadoc)
	 * @see cn.maszy.service.investment.B_T_Ufp_AdminService#selectOne(java.lang.String)
	 */
	public B_T_Ufp_EngineerMaterial selectOne(String uuid) {
		// TODO Auto-generated method stub
		B_T_Ufp_EngineerMaterial record=b_T_Ufp_EngineerMaterialMapper.selectByPrimaryKey(uuid);
		return record;
	}

	/* (non-Javadoc)
	 * @see cn.maszy.service.investment.B_T_Ufp_AdminService#delete(java.lang.String)
	 */
	public int delete(String checkIds) {
		// TODO Auto-generated method stub
		for (String id : checkIds.split(", ")) {
			if(!"on".equals(id))
			{
			B_T_Ufp_EngineerMaterial record=b_T_Ufp_EngineerMaterialMapper.selectByPrimaryKey(id);
			b_T_Ufp_EngineerMaterialMapper.deleteByPrimaryKey(id);
			//同时修改项目总价格
			//根据uuid 查询到所属的工程id
			float price =totalamount(record);
			B_T_Ufp_Engineering b_T_Ufp_Engineering =new B_T_Ufp_Engineering();
			b_T_Ufp_Engineering.setUuid(record.getEngineeringUuid());
			b_T_Ufp_Engineering.setMaterialPrice(price);
			b_T_Ufp_EngineeringMapper.updateByPrimaryKeySelective(b_T_Ufp_Engineering);
			}
		}
		return 1;
	}

	/* (non-Javadoc)
	 * @see cn.maszy.service.investment.B_T_Ufp_AdminService#count(cn.maszy.model.investment.B_T_Ufp_AdminExample)
	 */
	public int count(B_T_Ufp_EngineerMaterialExample example,B_T_Ufp_EngineerMaterialMapper b_T_Ufp_EngineerMaterialMapper) {
		// TODO Auto-generated method stub
		return b_T_Ufp_EngineerMaterialMapper.countByExample(example);
	}

	/* (non-Javadoc)
	 * @see cn.maszy.service.investment.B_T_Ufp_AdminService#selectList(cn.maszy.model.investment.B_T_Ufp_EngineerMaterial)
	 */
	public Pager<B_T_Ufp_EngineerMaterial> selectList(B_T_Ufp_EngineerMaterial b_T_Ufp_EngineerMaterial,Pager<B_T_Ufp_EngineerMaterial> pager) {
		// TODO Auto-generated method stub
		// 添加查询条件
		B_T_Ufp_EngineerMaterialExample example=new B_T_Ufp_EngineerMaterialExample();
		Criteria criteria = example.createCriteria();
		if(b_T_Ufp_EngineerMaterial!=null){
			if(b_T_Ufp_EngineerMaterial.getEngineeringUuid()!=null&&!"".equals(b_T_Ufp_EngineerMaterial.getEngineeringUuid())){
				criteria.andEngineeringUuidEqualTo(b_T_Ufp_EngineerMaterial.getEngineeringUuid());
			}
//			if(b_T_Ufp_EngineerMaterial.getStatus()!=null&&!"".equals(b_T_Ufp_EngineerMaterial.getStatus())){
//				criteria.andStatusEqualTo(b_T_Ufp_EngineerMaterial.getStatus());
//			}
//			if(b_T_Ufp_EngineerMaterial.getRealName()!=null&&!"".equals(b_T_Ufp_EngineerMaterial.getRealName())){
//				criteria.andRealNameLike("%"+B_T_Ufp_EngineerMaterial.getRealName()+"%");
//			}
		}else{
			criteria.andUuidIsNotNull();
		}
		example.or(criteria);
//		int totalCount=new B_T_Ufp_EngineerMaterialServiceImpl().count(example,b_T_Ufp_EngineerMaterialMapper);
//		pager.setTotalRecords(totalCount);
//		example.setLimitStart(pager.getStartRecord());
//		example.setLimitEnd(pager.getPageSize());
		example.setOrderByClause(pager.getOrderField()+" "+pager.getOrderDirection());
		
		// 取得总的记录条数
		List<B_T_Ufp_EngineerMaterial>  list=b_T_Ufp_EngineerMaterialMapper.selectByExample(example);
		pager.setPageRecords(list);
		return pager;
	}
	public List<B_T_Ufp_EngineerMaterial> select(B_T_Ufp_EngineerMaterial b_T_Ufp_EngineerMaterial)
	{
		B_T_Ufp_EngineerMaterialExample example=new B_T_Ufp_EngineerMaterialExample();
		Criteria criteria = example.createCriteria();
		if(b_T_Ufp_EngineerMaterial!=null){
			if(b_T_Ufp_EngineerMaterial.getEngineeringUuid()!=null&&!"".equals(b_T_Ufp_EngineerMaterial.getEngineeringUuid())){
				criteria.andEngineeringUuidEqualTo(b_T_Ufp_EngineerMaterial.getEngineeringUuid());
			}
		}else
		{
			criteria.andUuidIsNotNull();
		}
		example.or(criteria);
		// 取得总的记录条数
		List<B_T_Ufp_EngineerMaterial>  list=b_T_Ufp_EngineerMaterialMapper.selectByExample(example);
		return list;
	}
	//计算总金额
		public float totalamount(B_T_Ufp_EngineerMaterial b_T_Ufp_EngineerMaterial)
		{
			B_T_Ufp_EngineerMaterialExample example=new B_T_Ufp_EngineerMaterialExample();
			String sql = "SELECT SUM(total_amount)AS total_amount FROM b_T_Ufp_EngineerMaterial WHERE  engineering_uuid ='"+b_T_Ufp_EngineerMaterial.getEngineeringUuid()+"'";
			example.setOrderByClause(sql);
			List<B_T_Ufp_EngineerMaterial>  list=b_T_Ufp_EngineerMaterialMapper.selectByExampleby(example);
			Float i=(float) 0;
			
			if(list.get(0) == null)
			{
				return 0;
			}
			else
			{
				i = list.get(0).getTotalAmount();
			}
			return i;
		}
}
